package com.gdou.teaching.service.impl;

import com.gdou.teaching.Enum.ResultEnum;
import com.gdou.teaching.dataobject.Achievement;
import com.gdou.teaching.dataobject.Clazz;
import com.gdou.teaching.dataobject.CourseMaster;
import com.gdou.teaching.dataobject.User;
import com.gdou.teaching.exception.TeachingException;
import com.gdou.teaching.mapper.AchievementMapper;
import com.gdou.teaching.mapper.CourseMasterMapper;
import com.gdou.teaching.service.AchievementService;
import com.gdou.teaching.service.ClazzService;
import com.gdou.teaching.service.UserService;
import com.gdou.teaching.vo.ClazzVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bo
 * @date Created in 13:15 2019/9/12
 * @description
 **/
@Service
@Slf4j
public class AchievementServiceImpl implements AchievementService {
    @Autowired
    AchievementMapper achievementMapper;
    @Autowired
    CourseMasterMapper courseMasterMapper;
    @Autowired
    ClazzService clazzService;
    @Autowired
    UserService userService;

    @Override
    @Transactional
    public boolean addAchievementByClazzId(Integer courseId, Integer clazzId) {
        CourseMaster courseMaster = courseMasterMapper.selectCourseMasterByCourseId(courseId);
        List<Integer> students = clazzService.getStudentByClazzId(clazzId);
        if (students==null){
            log.error("添加课程,班级参数错误,不存在班级");
            throw new TeachingException(ResultEnum.PARAM_ERROR);
        }
//        //判断是否已存在成绩记录
//        List<Integer> clazzList = achievementMapper.getClazzIdListByCourseId(courseId);
//        if (clazzList.contains(clazzId)){
//            log.error("添加课程,该班级已存在");
//            throw new TeachingException(ResultEnum.PARAM_ERROR);
//        }
        try{
            for (int i=0;i<students.size();i++){
                Achievement achievement = new Achievement();
                achievement.setUserId(students.get(i));
                achievement.setClassId(clazzId);
                achievement.setCourseAchievement(0.0);
                achievement.setCourseName(courseMaster.getCourseName());
                achievement.setTeacherId(courseMaster.getUserId());
                achievement.setCourseId(courseMaster.getCourseId());
                achievementMapper.addAchievement(achievement);
            }
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
        return true;
    }


    @Override
    public boolean addAchievementByStudentList(Integer courseId, List<String> studentIdList) {
        CourseMaster courseMaster = courseMasterMapper.selectCourseMasterByCourseId(courseId);
        if (courseMaster==null){{
            log.error("添加课程,该课程不存在,courseId={}",courseId);
            throw new TeachingException(ResultEnum.COURSE_NOT_EXIST);
        }}
        for(int i=0;i<studentIdList.size();i++){
            String studentId=studentIdList.get(i);
            User student = userService.getUserByUserNumber(studentId);
            if (student==null){
                log.error("添加课程,该学生不存在或状态异常,studentId={}",studentId);
                throw new TeachingException(ResultEnum.PARAM_ERROR);
            }
            //判断是否已存在成绩记录
            Achievement exist =
                    achievementMapper.selectAchievementByUserIdAndCourserId(student.getUserId(), courseId);
            if (exist!=null){
                log.error("添加课程,该生已存在课程中,studentId={}",studentId);
                    throw new TeachingException(ResultEnum.PARAM_ERROR);
            }
            try{
                Achievement achievement = new Achievement();
                achievement.setUserId(student.getUserId());
                achievement.setClassId(student.getClassId());
                achievement.setCourseAchievement(0.0);
                achievement.setCourseName(courseMaster.getCourseName());
                achievement.setTeacherId(courseMaster.getUserId());
                achievement.setCourseId(courseMaster.getCourseId());
                achievementMapper.addAchievement(achievement);
            }catch (Exception e){
                log.error(e.getMessage());
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean deleteAchievementByStudentList(Integer courseId, List<String> studentIdList) {
        CourseMaster courseMaster = courseMasterMapper.selectCourseMasterByCourseId(courseId);
        if (courseMaster==null){{
            log.error("删除学生课程记录,该课程不存在,courseId={}",courseId);
            throw new TeachingException(ResultEnum.COURSE_NOT_EXIST);
        }}
        for(int i=0;i<studentIdList.size();i++){
            String studentId=studentIdList.get(i);
            User student = userService.getUserByUserNumber(studentId);
            if (student==null){
                log.error("删除学生课程记录,该学生不存在或状态异常,studentId={}",studentId);
                throw new TeachingException(ResultEnum.PARAM_ERROR);
            }
            //判断是否已存在成绩记录
            Achievement exist =
                    achievementMapper.selectAchievementByUserIdAndCourserId(student.getUserId(), courseId);
            if (exist!=null){
                log.error("删除学生课程记录,该生不在课程中,studentId={}",studentId);
                throw new TeachingException(ResultEnum.PARAM_ERROR);
            }
            try{
                achievementMapper.deleteAchievementByCourseIdAndUserId(courseId,student.getUserId());
            }catch (Exception e){
                log.error(e.getMessage());
                return false;
            }
        }
        return true;
    }

    @Override
    public List<ClazzVO> getClazzIdListByCourseId(Integer courseId) {
        List<Integer> list = achievementMapper.getClazzIdListByCourseId(courseId);
        List<ClazzVO> result = list.stream().map(id -> {
            Clazz classByClazzId = clazzService.getClassByClazzId(id);
            ClazzVO clazzVO = new ClazzVO();
            clazzVO.setClassId(id);
            clazzVO.setClassName(classByClazzId.getClassName());
            return clazzVO;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public Integer getCourseNumberByUserId(Integer userId) {
        Integer countByUserId = achievementMapper.getCountByUserId(userId);
        return countByUserId;
    }

    @Override
    public Achievement getAchievementByUserIdAndCourseId(Integer userId, Integer courseId) {
        Achievement achievement = achievementMapper.selectAchievementByUserIdAndCourserId(userId, courseId);
        return achievement;
    }
}
